package com.mapapp.flutter_mapapp.persenter;

import android.content.Context;

import com.mapapp.flutter_mapapp.bean.DetailTree;
import com.mapapp.flutter_mapapp.http.ActivityLifeCycleEvent;
import com.mapapp.flutter_mapapp.http.Api;
import com.mapapp.flutter_mapapp.http.HttpUtil;
import com.mapapp.flutter_mapapp.http.ProgressSubscriber;
import com.mapapp.flutter_mapapp.util.ToastUtils;
import com.mapapp.flutter_mapapp.view.RegionSelectionViewInterface;

import java.util.List;

import rx.subjects.PublishSubject;

public class RegionSelectionPresenter {
    private Context mContext;
    private RegionSelectionViewInterface mView;
    private PublishSubject<ActivityLifeCycleEvent> lifecycleSubject;

    public RegionSelectionPresenter(RegionSelectionViewInterface view, Context context, PublishSubject<ActivityLifeCycleEvent> lifecycleSubject) {
        this.mView = view;
        this.mContext = context;
        this.lifecycleSubject = lifecycleSubject;
    }

    public void getTreeList(final String taskId, final String parentId) {
        HttpUtil.getInstance().toSubscribe(Api.getApiService().getTree(taskId, parentId),
                new ProgressSubscriber<List<DetailTree>>(mContext, false) {
                    @Override
                    protected void _onNext(List<DetailTree> o) {
                        mView.showTreeList(o);
                    }

                    @Override
                    protected void _onError(String message) {
                        ToastUtils.showMessage(mContext, message);
                    }
                }, lifecycleSubject);
    }

    public void updateNode(final String taskId, final String id) {
        HttpUtil.getInstance().toSubscribe(Api.getApiService().getTree(taskId, id),
                new ProgressSubscriber<List<DetailTree>>(mContext, false) {
                    @Override
                    protected void _onNext(List<DetailTree> o) {
                        mView.showUpdateNode(o);
                    }

                    @Override
                    protected void _onError(String message) {
                        ToastUtils.showMessage(mContext, message);
                    }
                }, lifecycleSubject);
    }


    public void getTreeList(final String parentId) {
        HttpUtil.getInstance().toSubscribe(Api.getApiService().getTreesByParentId(parentId),
                new ProgressSubscriber<List<DetailTree>>(mContext, false) {
                    @Override
                    protected void _onNext(List<DetailTree> o) {
                        mView.showTreeList(o);
                    }

                    @Override
                    protected void _onError(String message) {
                        ToastUtils.showMessage(mContext, message);
                    }
                }, lifecycleSubject);
    }


}
